﻿using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
using System.Threading.Tasks;

namespace MiddleWare
{
    public class Middleware1
    {
        private readonly RequestDelegate _next;
        public Middleware1(RequestDelegate next)
        {
            _next = next;
        }
        public Task InvokeAsync(HttpContext context)
        {
            //这里可以对请求所有位置做操作
            //context.Request.Headers
            //context.Request.Query
            //context.Request.Body
            if (!context.Items.ContainsKey("M_test"))
            {
                context.Items.Add("M_test", "");
            }
            context.Items["M_test"] = $"{context.Items["M_test"]} Middleware1 ";
            return this._next(context);
        }
    }
    /// <summary>
    /// 增加一个
    /// </summary>
    public static class UseMiddleware1Extensions
    {
        public static IApplicationBuilder UseMiddleware1(this IApplicationBuilder builder)
        {
            return builder.UseMiddleware<Middleware1>();
        }
    }
}
